/*
 * CMusic.h
 *
 *  Created on: 18.2.2009
 *      Author: jari.ravantti
 */

#ifndef CMUSIC_H_
#define CMUSIC_H_
#include <SDL_mixer.h>
#include <string>
#include <iostream>
#include "../defines/defines.h"
#ifdef DEBUG
#include "../logger/CLogger.h"
#endif
class CMusic
{
private:
    Mix_Music *m_musicFile;
    std::string m_FileName;

public:
    virtual ~CMusic()
    {

        Mix_FreeMusic(m_musicFile); // free musicfile from memory
#ifdef DEBUG
        CLogger::Instance()->log(LOG_LEVEL_NOTICE,"CMusic::~CMusic : filename %s deleted",m_FileName.c_str());
#endif

    }
    bool Init()
    {
        SDL_Init(SDL_INIT_AUDIO);

        if (Mix_OpenAudio(FREQUENCY, MIX_DEFAULT_FORMAT, CHANNELS, CHUNKSIZE))
        {
#ifdef DEBUG
            CLogger::Instance()->log(LOG_LEVEL_ERROR,"CMusic::Init : Unable to init audio!\n");
#endif

            return false;
        }
        return true;
    }
    void Close()
    {
        Mix_FreeMusic(m_musicFile); // free musicfile from memory
#ifdef DEBUG
        CLogger::Instance()->log(LOG_LEVEL_NOTICE,"CMusic::Close : Filename %s closed",m_FileName.c_str());
#endif
    }


    //load music file
    bool LoadMusic(const std::string filename)
    {
        m_musicFile = Mix_LoadMUS(filename.c_str());
        if (m_musicFile == NULL)
        {
            return false;
        }
        m_FileName = filename;
#ifdef DEBUG
        CLogger::Instance()->log(LOG_LEVEL_NOTICE,"CMusic::LoadMusic : Filename %s loaded",m_FileName.c_str());
#endif
        return true;
    }

    //play & resume music
    bool Play()
    {


        if (Mix_PlayingMusic() == 0)   //Play the music
        {
            if (Mix_PlayMusic(m_musicFile, -1) == -1)
            {

#ifdef DEBUG
                CLogger::Instance()->log(LOG_LEVEL_NOTICE,"CMusic::Play : Playing  %s ",m_FileName.c_str());
#endif
                return true;
            }
        }
        //If music is being played
        else
        {
            //If the music is paused
            if (Mix_PausedMusic() == 1)
            {
                //Resume the music
#ifdef DEBUG
                CLogger::Instance()->log(LOG_LEVEL_NOTICE,"CMusic::Play : Resuming  %s ",m_FileName.c_str());
#endif
                Mix_ResumeMusic();
                return true;
            }
        }
        //std::cerr << "Error playing music" << std::endl;
        return false;
    }

    //stop music
    bool Stop()
    {
        if (Mix_PlayingMusic() == 1)
        {
#ifdef DEBUG
            CLogger::Instance()->log(LOG_LEVEL_NOTICE,"CMusic::Stop : Halted  %s ",m_FileName.c_str());
#endif
            Mix_HaltMusic();
            return true;
        }
        return false;
    }

    //pause music
    bool Pause()
    {
        if (Mix_PlayingMusic() == 1)
        {
#ifdef DEBUG
            CLogger::Instance()->log(LOG_LEVEL_NOTICE,"CMusic::Pause : Paused  %s ",m_FileName.c_str());
#endif
            Mix_PauseMusic();
            return true;
        }
        return false;
    }

    bool isPlaying()
    {
        if (Mix_PlayingMusic() == 1)
        {
            return true;
        }
        else return false;
    }

    bool isPaused()
    {
        if (Mix_PausedMusic() == 1)
        {
            return true;
        }
        else return false;
    }
    std::string GetSongName()
    {
        return this->m_FileName;
    }
};

#endif /* CMUSIC_H_ */
